package br.com.droid;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import br.com.droid.model.Telefone;

public class BancoTelefone {

	private static BancoTelefone instance;
	private ArrayList<Telefone> listaTelefones;
	
	public BancoTelefone() {
		listaTelefones = new ArrayList<Telefone>();
	}
	
	public static BancoTelefone getBancoInstance(){
		if(instance==null)
			instance = new BancoTelefone();
		return instance;
	}
		
	public Telefone buscarTelefone(int id){
		Telefone telefone = null;
		String teste = "SELECT * from Telefone where id = +"+id+";";
        
        if(ConexaoMySQL.getConexaoMySQL()!=null){
            Statement stm;
			try {
				stm = ConexaoMySQL.getConexaoMySQL().createStatement();
				ResultSet rs = stm.executeQuery(teste);
		            while (rs.next()) {
		                telefone = new Telefone();
		                telefone.setId(rs.getLong("id"));
		                telefone.setId_centro(rs.getLong("id_centro"));
		        		telefone.setNumero(rs.getString("numero"));		        		
		            }
	            ConexaoMySQL.FecharConexao();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		return telefone;
	}
	
	public ArrayList<Telefone> getListaTelefones(){
        String teste = "SELECT * from Telefone;";
        listaTelefones.clear();
        
        if(ConexaoMySQL.getConexaoMySQL()!=null){ 
            Statement stm;
			try {
				stm = ConexaoMySQL.getConexaoMySQL().createStatement();
				ResultSet rs = stm.executeQuery(teste);
		            while (rs.next()) {
		            	Telefone telefone = new Telefone();
		                telefone.setId(rs.getLong("id"));
		                telefone.setId_centro(rs.getLong("id_centro"));
		        		telefone.setNumero(rs.getString("numero"));
		        		listaTelefones.add(telefone);
		            }
	            ConexaoMySQL.FecharConexao();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		return listaTelefones;
	}
}
